package restless.core.managers {
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import restless.core.RLEXMLData;
	/**
	 * ...
	 * @author MNS
	 */
	public class RLEXMLManager {
		
		private var xml:XML;
		private var tempXMLData:RLEXMLData;
		private var tempXMLList:XMLList;
		private var loader:URLLoader;
		private var ids:Vector.<String> = new Vector.<String>;
		private var data:Vector.<RLEXMLData> = new Vector.<RLEXMLData>;
		
		public function RLEXMLManager() {
			
		}
		
		private function loaded(e:Event):void {
			xml = new XML(e.target.obj);
			trace(xml);
			parseXML();
		}
		
		public function loadXML(xmlURL:String):void {
			var request:URLRequest = new URLRequest("fp.xml");
			loader = new URLLoader(request);
			loader.addEventListener(Event.INIT, loaded);
			loader.load(request);
		}
		
		public function getXMLData(sID:String):RLEXMLData {
			var tempString:String = sID;
			var i:int = ids.length - 1;
			for (i; i > -1; i--) {
				if (ids[i] == tempString) {
					tempXMLData = data[i].clone();
					return tempXMLData;
				}
			}
			return null;
		}
		
		private function parseXML():void {
			
			
			
			var i:int = xml.length-1;
			for (i; i > -1; i--) {
				ids.push( xml[i].toString() );
				tempXMLData = new RLEXMLData();
				tempXMLList = xml[i];
				var j:int = tempXMLList.length;
				for (j; j > -1; j--) {
					tempXMLData.ids.push( tempXMLList[j].toString() );
					tempXMLData.data.push( tempXMLList[j] );
				}
				data.push(tempXMLData);
			}
		}
		
		public function clear():void {
			ids.length = 0;
			data.length = 0;
			xml = null;
		}
	}
}